﻿/*************************************************************

                    EffectTween
                                  
//-----------------------------------------------------------								*/
import mx.transitions.Tween;
//注意   要给系统类库里的Tween类填加    public var constructor:Function    才可使用
//另一种不需要向Tween添加 constructor的方法在下面有详细说明。
class Silly.Effect.EffectTween
{
	public var addListener:Function;
	public var removeListener:Function;
	private var broadcastMessage:Function;
	private var _listeners:Array;
	//为类添加事件通知和侦听器管理功能
	private var _aim:MovieClip;
	//应用效果的对象
	private var _startX:Number;
	private var _startY:Number;
	//起始水平和垂直模糊量  0 到 255
	private var _endX:Number;
	private var _endY:Number;
	//结束水平和垂直模糊量  0 到 255
	private var _currentX:Number;
	private var _currentY:Number;
	//当前水平和垂直模糊量  0 到 255s
	private var _tweenX:Tween;
	private var _tweenY:Tween;
	//水平和垂直缓动对象
	private var _quality:Number;
	//显示精度 1 到 15
	private var _currentFunc:Function;
	//当前使用的缓动函数
	private var _duration:Number;
	//动画持续时间
	private var _useSeconds:Boolean;
	//是否使用秒为记时单位 true为秒  flash为帧频
	private var constructor:Function;
	//定义构造器
	public var onFinished:Function;
	//动画结束事件监听器
	private var _isEnd:Boolean;
	//动画结束标志
	public function EffectTween ()
	{
		AsBroadcaster.initialize (this);
		this._listeners = [];
		this.addListener (this);
		this._quality = 3;
		this._useSeconds = false;
		this._isEnd = true;
		this._currentFunc = function (t, b, c, d)
		{
			return c * t / d + b;
		};
		this._tweenX = new Tween (this, this._currentX, this._currentFunc, this._startX, this._endX, this._duration, this._useSeconds);
		this._tweenY = new Tween (this, this._currentY, this._currentFunc, this._startY, this._endY, this._duration, this._useSeconds);
		//初始化属性
	}
	public function start ():Void
	{
		//开始播放效果
		this._tweenX.constructor(this, this._currentX, this._currentFunc, this._startX, this._endX, this._duration, this._useSeconds);
		this._tweenY.constructor(this, this._currentY, this._currentFunc, this._startY, this._endY, this._duration, this._useSeconds);
		//重新构造控制水平和垂直方向的缓动对象
		//this._tweenX = new Tween (this, this._currentX, this._currentFunc, this._startX, this._endX, this._duration, this._useSeconds);
		//this._tweenY = new Tween (this, this._currentY, this._currentFunc, this._startY, this._endY, this._duration, this._useSeconds);
		//用new 重新构造Tween 和用constructor 重新构造不知道有没有性能上的区别，
		//如果选择用 new 就不需要为 Tween 添加 constructor .
		this._tweenX.addListener (this);
		this._tweenY.addListener (this);
		//监听 Tween 发出的事件
		this._isEnd = false;
	}
	public function pause ():Void
	{
		//暂停效果的播放
		this._tweenX.stop ();
		this._tweenY.stop ();
	}
	public function resume ():Void
	{
		//继续播放
		this._tweenX.resume ();
		this._tweenY.resume ();
	}
	private function onMotionChanged (a:Tween, b:Number):Void
	{
		//接收tween发出的  onMotionChanged
		if (a == this._tweenX) {
			this._currentX = b;
		} else {
			this._currentY = b;
		}
		//将不同Tween对象返回的值符值给相应的坐标
		this.broadcastMessage ("onChanged", this);
	}
	private function effectToMc (mc:MovieClip, xNum:Number, yNum:Number):Void
	{
		//预留的将效果加载到mc的方法
	}
	private function onChanged (a:EffectTween):Void
	{
		//接收自己发出的 onChanged
		if (a == this) {
			this.effectToMc (this._aim, this._currentX, this._currentY);
			//刷新屏幕
		}
	}
	private function onMotionFinished (a:Tween):Void
	{
		//接收tween发出的  onMotionFinished 
		if (a == this._tweenX) {
			//只接收一个_tweenX 的onFinished  因为_tweenX和_tweenY是同步结束的
			this._isEnd = true;
			this.broadcastMessage ("onFinished", this);
			//发出动画结束事件
		}
	}
	//======================================================================
	public function set startX (num:Number):Void
	{
		this._startX = num;
	}
	public function get startX ():Number
	{
		return this._startX;
	}
	public function set startY (num:Number):Void
	{
		this._startX = num;
	}
	public function get startY ():Number
	{
		return this._startY;
	}
	//--------------------------------
	public function set endX (num:Number):Void
	{
		this._endX = num;
	}
	public function get endX ():Number
	{
		return this._endX;
	}
	public function set endY (num:Number):Void
	{
		this._endX = num;
	}
	public function get endY ():Number
	{
		return this._endY;
	}
	//========================================================================
	public function set aim (mc:MovieClip):Void
	{
		this._aim = mc;
	}
	public function get aim ():MovieClip
	{
		return this._aim;
	}
	public function set quality (num:Number):Void
	{
		this._quality = num;
	}
	public function get quality ():Number
	{
		return this._quality;
	}
	public function set duration (num:Number):Void
	{
		this._duration = num;
	}
	public function get duration ():Number
	{
		return this._duration;
	}
	public function set currentFunc (func:Function):Void
	{
		this._currentFunc = func;
	}
	public function get currentFunc ():Function
	{
		return this._currentFunc;
	}
	public function set useSeconds (b:Boolean):Void
	{
		this._useSeconds = b;
	}
	public function get useSeconds ():Boolean
	{
		return this._useSeconds;
	}
}
